package com.homework2;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.HashMap;
import java.util.Properties;

public class homeworker2 {
    public static void main(String[] args) throws IOException {

        String filepath2 = "src\\dog.properties";
        Properties properties = new Properties();
        properties.load(new FileInputStream(filepath2));
        String name = properties.getProperty("name");
        int age = Integer.parseInt(properties.getProperty("age"));
        String color = properties.getProperty("color");
        Dog dog = new Dog(name, age, color);
        System.out.println(dog);

        String filepath3 = "E:\\Java\\java_code\\mytemp\\dog.dat";
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filepath3));
        objectOutputStream.writeObject(dog);
        objectOutputStream.close();
        System.out.println("object流输入成功");//序列化

/*        HashMap<String, String> a = new HashMap<>();//有点忘记Hashmap了
        a.put("name", name);
        a.put("sds",name);
        String b=a.get("name");*/

    }

@Test
    public void seritest() throws IOException, ClassNotFoundException {//反序列化读取
    String filepath = "E:\\Java\\java_code\\mytemp\\dog.dat";
    ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filepath));
    Dog dog =(Dog)objectInputStream.readObject();//记得要向下转型
    System.out.println(dog);
    objectInputStream.close();
}
}

class Dog implements Serializable {
    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }

    private String name;
    private int age;
    private String color;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
